<?php
class IssueHandler {
	public static function fixIssueCount($iJob) {
		try {
			$oQuery = DataHandler::query("
				SELECT COUNT(*)
				FROM issues
				WHERE job_id = ?
			", $iJob);

			list($iCount) = $oQuery->fetch(PDO::FETCH_NUM);

			DataHandler::query("
				UPDATE jobs
				SET issue_count = ?
				WHERE id = ?
			", $iCount, $iJob);

			return true;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function denyIssue($iJob, $iIssue) {
		try {
			$oQuery = DataHandler::query("
				DELETE FROM issues
				WHERE job_id = ?
				AND id = ?
			", $iJob, $iIssue);

			IssueHandler::fixIssueCount($iJob);

			return 1;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function acceptIssue($iJob, $iIssue, $iMissiongroup, $sTodo) {
		try {
			$oIssues = IssueHandler::getIssues($iJob);
			$oIssue = $oIssues[$iIssue];

			DataHandler::beginTransaction();
			
			$oQuery = DataHandler::query("
				INSERT INTO missions (
					todo,
					requester,
					group_id,
					job_id,
					requesttime,
					users,
					assigntime
				) VALUES (
					?,
					?,
					?,
					?,
					?,
					?,
					?
				)
			", ($sTodo ? $sTodo : $oIssue->sBody), $oIssue->iUser, $iMissiongroup, $iJob, $oIssue->iTiming, $oIssue->iReceiver, thisSecond());

			$iNewID = DataHandler::lastInsertId();

			if ($sTodo) {
				DataHandler::query("
					INSERT INTO notes (
						job_id,
						mission_id,
						user_id,
						body,
						timing
					) VALUES (
						?,
						?,
						?,
						?,
						?
					)
				", $iJob, $iNewID, $oIssue->iUser, $oIssue->sBody, $oIssue->iTiming);
			}

			DataHandler::query("
				DELETE FROM issues
				WHERE id = ?
			", $iIssue);

			DataHandler::commit();
			
			IssueHandler::fixIssueCount($iJob);

			return $iNewID;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
	
	public static function addIssue($iJob, $iReceiver, $sBody) {
		try {
			if (!$sBody) {
				throw new MiJobException('Body must contain text');
			}

			$oQuery = DataHAndler::query("
				INSERT INTO issues (
					job_id,
					user_id,
					receiver_id,
					body,
					timing
				) VALUES (
					?,
					?,
					?,
					?,
					?
				)
			", $iJob, $_SESSION['rid'], $iReceiver, $sBody, thisSecond());

			$iNewID = DataHandler::lastInsertId();

			IssueHandler::fixIssueCount($iJob);

			return $iNewID;
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}

	public static function getIssues($iJob) {
		try {
			if (@is_array($GLOBALS['issues'][$iJob])) {
				return $GLOBALS['issues'][$iJob];
			}

			$oQuery = DataHandler::query("
				SELECT
					id,
					user_id,
					receiver_id,
					body,
					timing
				FROM issues
				WHERE job_id = ?
				ORDER BY timing DESC
			", $iJob);

			$GLOBALS['issues'][$iJob] = array();
			while (list($iID, $iUser, $iReceiver, $sBody, $iTiming) = $oQuery->fetch(PDO::FETCH_NUM)) {
				$GLOBALS['issues'][$iJob][$iID] = new Issue($iID, $iUser, $iReceiver, $sBody, $iTiming);
			}

			return $GLOBALS['issues'][$iJob];
		}
		catch (Exception $e) {
			ErrorHandler::addError($e->getMessage());
		}
	}
}
?>
